//logs.js
const util = require('../../utils/util.js')
// import constant from '../../constant/constant.js'
Page({
  data: {
    logs: [],
    userInfo: global.$cacheHelper.appUser(),
    typeWork: [],
    isCompliance: false,
    roleType: false,
  },
  changePsd() {
    if (this.data.userInfo == null) {
      global.$cacheHelper.tologin()
    } else {
      wx.navigateTo({
        url: './changepsd/changepsd',
      })
    }

  },
  async getTypeWork() {
    if (this.data.typeWork.length != 0) {
      return
    }
    var data = await global.$api.post({
      url: 'api/userInfo/getMyOccupation',
      data: {}
    })
    if (data.code == 200) {
      this.setData({
        typeWork: data.data
      })
      console.log(data.data)
      // data.data.forEach(res=>{
      //   if(res.type==2){
      //     getApp().globalData.exchange=res.type
      //   }else if(res.type==3){
      //     getApp().globalData.retreat=res.type
      //   }
      //   console.log(getApp().globalData)
      // })
    }
  },
  //登录
  Login() {
    if (this.data.userInfo == null) {
      global.$cacheHelper.tologin()
    } else {
      return
    }
  },
  //考勤打卡
  hanleAttendance() {
    wx.navigateTo({
      url: '../index/scan/scan?type=99',
    })
  },
  // 厂规监管
  hanleToCGJG() {
    if (this.data.userInfo == null) {
      global.$cacheHelper.tologin()
    } else {
      wx.navigateTo({
        url: '../index/changguiCheck/changguiCheck',
      })
    }
  },
  //退出登录
  logOut() {
    let that = this
    wx.showModal({
      title: '确认退出登录?',
      cancelColor: 'cancelColor',
      success(res) {
        if (res.confirm == true) {
          wx.removeStorageSync('app_login_token')
          wx.removeStorageSync('app_user_info')
          that.setData({
            userInfo: global.$cacheHelper.appUser(),
            typeWork: []
          })
        }
      }
    })
  },
  showAdvicePage() {
    if (this.data.userInfo == null) {
      global.$cacheHelper.tologin()
    } else {
      wx.navigateTo({
        url: './advice/advice',
      })
    }
  },
  scanCode() {
    console.log(111)
    // 质量检验扫码
    if (global.$cacheHelper.appUser() == null) {
      global.$cacheHelper.tologin()
    } else {
      // 跳转扫描
      wx.navigateTo({
        url: '../index/scan/scan?type=11&process=null',
      })
      //    wx.navigateTo({
      //   url: '../index/qualityCheck/qualityCheck?handOrderNum=多件测试',
      // })
    }
  },
  //获取用户信息
  async getUerInfo() {
    var data = await global.$api.post({
      url: 'api/userInfo/getUserInfo',
      data: {}
    })
    if (data.code == 200) {
      var user = data.data;
      // wx.setStorageSync(constant.StorageName.APP_USER_INFO, user);
      console.log(data.data);
      this.setData({
        isCompliance: user.isCompliance == '2' ? true : false,
        roleType: user.roleType == '3' ? true : false,
      })
    }
  },
  onLoad: function () {
    this.setData({
      logs: (wx.getStorageSync('logs') || []).map(log => {
        return util.formatTime(new Date(log))
      })
    })
    // let userInfo = JSON.parse(wx.getStorageSync('app_user_info'));
    // console.log(11111,userInfo) // 2
    // this.setData({
    //   isCompliance: userInfo.isCompliance == '2' ? true : false,
    //   roleType: userInfo.roleType == '3' ? true : false,
    // })
  },
  onShow() {
    this.getUerInfo();
    this.setData({
      userInfo: global.$cacheHelper.appUser()
    })
    if (this.data.userInfo != null && this.data.typeWork.length == 0) {
      this.getTypeWork()
    }
  }
})